from django.shortcuts import render
# 引入文件response函数
from django.http import FileResponse


# Create your views here.
def saveFileInDjango(request):
    # 根据用户请求的类型决定不同的响应
    # GET请求返回功能页面
    if request.method == 'GET':
        return render(request, 'testApp/save_file_in_django.html')
    # POST请求保存用户提交的文件、返回用户目标文件
    else:
        uploadFile = request.FILES["myfile"].read()
        with open("test.txt", 'wb') as sf:
            sf.write(uploadFile)
        # 在这里已经将文件保存了下来，可以进行处理了
        response = FileResponse(open("test.txt", 'rb'))
        response['Content-Type'] = 'application/octet-stream'
        response['Content-Disposition'] = "attachment;filename=test.txt"
        return response
